//
//  YKFarmFoodAddViewController.m
//  YK_BaseMediator
//
//  Created by edward on 2021/3/12.
//  Copyright © 2021 Edward. All rights reserved.
//

#import "YKFarmFoodAddViewController.h"
#import <Masonry/Masonry.h>
#import <YK_ExectionModel/YKNSObjectCore.h>
#import <YK_ExectionModel/YKUIKitCore.h>

@interface YKFarmFoodAddViewController ()
///
@property(nonatomic,strong) NSMutableDictionary *params;
///
@property(nonatomic,copy) void(^comfirmBlock)(NSDictionary *params);

///
@property(nonatomic,strong) UITextField *foodnameTextField;
///
@property(nonatomic,strong) UITextField *muchTextField;
///
@property(nonatomic,strong) UITextField *eattimeTextField;
///
@property(nonatomic,strong) UITextField *drugnameTextField;
///
@property(nonatomic,strong) UITextField *drugmuchTextField;
@end

@implementation YKFarmFoodAddViewController

- (instancetype)initWithMealTime:(NSString *)mealTime farmId:(NSString *)farmId comfirmBlock:(void(^)(NSDictionary *params))block
{
    self = [super init];
    if (self) {
        self.title = [mealTime isEqualToString:@"00"]?@"早餐":[mealTime isEqualToString:@"01"]?@"午餐":[mealTime isEqualToString:@"02"]?@"晚餐":[NSString stringWithFormat:@"加餐%@",mealTime];
        self.params = [@{@"MealTime":mealTime,@"farmId":farmId} mutableCopy];
        self.comfirmBlock = block;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIView *topView = UIView.instance.yk_borderColor(UIColor.blackColor);
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(44);
    }];
    UILabel *titleLabel = UILabel.instance;
    titleLabel.text = self.title;
    titleLabel.font = [UIFont systemFontOfSize:20];
    [topView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(topView.mas_centerY);
        make.centerX.mas_equalTo(topView.mas_centerX);
    }];
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:UIColor.blueColor forState:UIControlStateNormal];
    [topView addSubview:cancelButton];
    [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(44);
    }];
    UIButton *comfirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [comfirmButton setTitle:@"确定" forState:UIControlStateNormal];
    [comfirmButton setTitleColor:UIColor.blueColor forState:UIControlStateNormal];
    [topView addSubview:comfirmButton];
    [comfirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(44);
    }];
    
    UILabel *foodNameLb = UILabel.instance;
    foodNameLb.text = @"食物名:";
    foodNameLb.font = [UIFont systemFontOfSize:20];
    foodNameLb.textAlignment = NSTextAlignmentRight;
    [self.view addSubview:foodNameLb];
    [foodNameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).offset(10);
        make.top.mas_equalTo(topView.mas_bottom).offset(10);
        make.width.mas_equalTo((YKScreenW - 20)/2);
        make.height.mas_equalTo(24);
    }];
    [self.view addSubview:self.foodnameTextField];
    [self.foodnameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(foodNameLb.mas_top);
        make.left.mas_equalTo(foodNameLb.mas_right);
        make.right.mas_equalTo(self.view.mas_right).offset(-10);
        make.width.mas_equalTo(foodNameLb.mas_width);
        make.height.mas_equalTo(foodNameLb.mas_height);
    }];
    
    UILabel *muchLb = UILabel.instance;
    muchLb.text = @"料量:";
    muchLb.font = [UIFont systemFontOfSize:20];
    muchLb.textAlignment = NSTextAlignmentRight;
    [self.view addSubview:muchLb];
    [muchLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(foodNameLb.mas_bottom).offset(5);
        make.left.width.height.mas_equalTo(foodNameLb);
    }];
    [self.view addSubview:self.muchTextField];
    [self.muchTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.height.mas_equalTo(muchLb);
        make.right.mas_equalTo(self.foodnameTextField.mas_right);
    }];
    
    UILabel *eattimeLb = UILabel.instance;
    eattimeLb.text = @"食料时长:";
    eattimeLb.font = [UIFont systemFontOfSize:20];
    eattimeLb.textAlignment = NSTextAlignmentRight;
    [self.view addSubview:eattimeLb];
    [eattimeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(muchLb.mas_bottom).offset(5);
        make.left.width.height.mas_equalTo(muchLb);
    }];
    [self.view addSubview:self.eattimeTextField];
    [self.eattimeTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.height.mas_equalTo(eattimeLb);
        make.right.mas_equalTo(self.muchTextField.mas_right);
    }];
    
    UILabel *drugname = UILabel.instance;
    drugname.text = @"伴药:";
    drugname.font = [UIFont systemFontOfSize:20];
    drugname.textAlignment = NSTextAlignmentRight;
    [self.view addSubview:drugname];
    [drugname mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(eattimeLb.mas_bottom).offset(5);
        make.left.width.height.mas_equalTo(eattimeLb);
    }];
    [self.view addSubview:self.drugnameTextField];
    [self.drugnameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.height.mas_equalTo(drugname);
        make.right.mas_equalTo(self.muchTextField.mas_right);
    }];
    
    UILabel *drugmuch = UILabel.instance;
    drugmuch.text = @"伴药量:";
    drugmuch.font = [UIFont systemFontOfSize:20];
    drugmuch.textAlignment = NSTextAlignmentRight;
    [self.view addSubview:drugmuch];
    [drugmuch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(drugname.mas_bottom).offset(5);
        make.left.width.height.mas_equalTo(eattimeLb);
    }];
    [self.view addSubview:self.drugmuchTextField];
    [self.drugmuchTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.height.mas_equalTo(drugmuch);
        make.right.mas_equalTo(self.muchTextField.mas_right);
    }];
    
    
    
    @weakify(self);
    [[cancelButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    [[comfirmButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        if (self.comfirmBlock) {
            
            NSString *date;
            NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
            [fmt setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
            fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
            date = [fmt stringFromDate:[NSDate date]];
            NSString *str = [NSString stringWithFormat:@"%@",date];
            self.params[@"createTime"] =  str;
            self.params[@"foodname"] = self.foodnameTextField.text;
            self.params[@"much"] = self.muchTextField.text;
            self.params[@"eattime"] = self.eattimeTextField.text;
            self.params[@"drugname"] = self.drugnameTextField.text;
            self.params[@"drugmuch"] = self.drugmuchTextField.text;
            self.comfirmBlock(self.params);
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }];
    [self.view yk_tapOnView:^(UITapGestureRecognizer *tap) {
        @strongify(self);
        [self resignFirstResponder];
    }];
    
}

- (UITextField *)foodnameTextField
{
    if (!_foodnameTextField) {
        _foodnameTextField = (UITextField *)[[UITextField alloc] init].yk_borderColor(UIColor.blackColor);
        
    }
    return _foodnameTextField;
}

- (UITextField *)muchTextField
{
    if (!_muchTextField) {
        _muchTextField = (UITextField *)[[UITextField alloc] init].yk_borderColor(UIColor.blackColor);
        _muchTextField.keyboardType = UIKeyboardTypeDecimalPad;
    }
    return _muchTextField;
}

- (UITextField *)eattimeTextField
{
    if (!_eattimeTextField) {
        _eattimeTextField = (UITextField *)[[UITextField alloc] init].yk_borderColor(UIColor.blackColor);
    }
    return _eattimeTextField;
}

- (UITextField *)drugnameTextField
{
    if (!_drugnameTextField) {
        _drugnameTextField = (UITextField *)[[UITextField alloc] init].yk_borderColor(UIColor.blackColor);
    }
    return _drugnameTextField;
}

- (UITextField *)drugmuchTextField
{
    if (!_drugmuchTextField) {
        _drugmuchTextField = (UITextField *)[[UITextField alloc] init].yk_borderColor(UIColor.blackColor);
        _drugmuchTextField.keyboardType = UIKeyboardTypeDecimalPad;
    }
    return _drugmuchTextField;
}


@end
